package com.bytz.modules.cms.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytz.common.api.vo.Result;
import com.bytz.common.aspect.annotation.AutoLog;
import com.bytz.common.system.query.QueryGenerator;
import com.bytz.modules.cms.common.constants.AdminAuthConstants;
import com.bytz.modules.cms.order.entity.OrderFlowRuleTestCase;
import com.bytz.modules.cms.order.model.OrderFlowRuleTestCaseSaveRo;
import com.bytz.modules.cms.order.service.IOrderFlowRuleTestCaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@Api(tags = "订单流程规则设计测试用例")
@RequestMapping("/orderDesignFlowRuleTestCase")
public class OrderFlowRuleTestCaseController {

    @Autowired
    private IOrderFlowRuleTestCaseService orderFlowRuleTestCaseService;

    /**
     * 分页查询
     */
    @ApiOperation(value = "分页列表查询")
    @GetMapping(value = "/list")
    @PreAuthorize("hasAuthority('" + AdminAuthConstants.OrderFlowRuleTestCase.VIEW + "')")
    public Result<IPage<OrderFlowRuleTestCase>> queryPageList(OrderFlowRuleTestCase orderFlowRule,
                                                              @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                              HttpServletRequest req) {
        QueryWrapper<OrderFlowRuleTestCase> queryWrapper = QueryGenerator.initQueryWrapper(orderFlowRule, req.getParameterMap());
        Page<OrderFlowRuleTestCase> page = new Page<>(pageNo, pageSize);
        IPage<OrderFlowRuleTestCase> pageList = orderFlowRuleTestCaseService.queryPage(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 保存
     *
     * @param saveRo 测试用例
     */
    @AutoLog(value = "订单流程规则测试用例-保存")
    @ApiOperation(value = "保存")
    @PreAuthorize("hasAuthority('" + AdminAuthConstants.OrderFlowRuleTestCase.VIEW + "')")
    @PostMapping(value = "/save")
    public Result<OrderFlowRuleTestCaseSaveRo> save(@RequestBody @Valid OrderFlowRuleTestCaseSaveRo saveRo) {
        return Result.ok("保存成功！", orderFlowRuleTestCaseService.saveOrUpdateSaveObject(saveRo, true));
    }

    /**
     * 通过id删除
     */
    @AutoLog(value = "订单流程规则测试用例-通过id删除")
    @ApiOperation(value = "通过id删除")
    @PreAuthorize("hasAuthority('" + AdminAuthConstants.OrderFlowRuleTestCase.EDIT + "')")
    @PostMapping(value = "/delete")
    public Result<Boolean> delete(@RequestParam(name = "id") String id) {
        orderFlowRuleTestCaseService.checkAndRemoveById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 通过id查询
     */
    @ApiOperation(value = "通过id查询")
    @PreAuthorize("hasAuthority('" + AdminAuthConstants.OrderFlowRuleTestCase.VIEW + "')")
    @GetMapping(value = "/queryById")
    public Result<OrderFlowRuleTestCaseSaveRo> queryById(@RequestParam(name = "id") String id) throws Exception {
        return Result.ok(orderFlowRuleTestCaseService.getSaveRoById(id));
    }

}
